package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.admin.service.ChannelService;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.common.excpetion.CustomException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service("ChannelService")
@Transactional(readOnly = true)
public class ChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements ChannelService {

    /**
     * 查询频道列表
     *
     * @param channelDTO
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDTO channelDTO) {

        if (channelDTO == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        //检查分页参数
        channelDTO.checkParam();

        Page<AdChannel> page = new Page<>(channelDTO.getPage(), channelDTO.getSize());

        LambdaQueryWrapper<AdChannel> lambdaQuery = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(channelDTO.getName()))
            lambdaQuery.like(AdChannel::getName, channelDTO.getName());

        lambdaQuery.orderByAsc(AdChannel::getOrd);
        //按结果查询
        IPage<AdChannel> result = super.page(page, lambdaQuery);
        PageResponseResult pageResponseResult = new PageResponseResult(channelDTO.getPage(), channelDTO.getSize(), result.getTotal());

        pageResponseResult.setData(result.getRecords());
        return pageResponseResult;
    }

    /**
     * 新增频道
     *
     * @param adChannel 前端请求数据
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public ResponseResult saveChannel(AdChannel adChannel) {

        if (adChannel == null || StringUtils.isNotEmpty(adChannel.getName()) || adChannel.getName().length() > 10)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);


        LambdaUpdateWrapper<AdChannel> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        //查询数据库中是否已经存在
        lambdaUpdateWrapper.eq(AdChannel::getName, adChannel.getName());

        //频道已存在
        if (super.count(lambdaUpdateWrapper) > 0)
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);

        //添加创建时间
        adChannel.setCreatedTime(new Date());
        //新增数据
        super.save(adChannel);

        return ResponseResult.okResult();
    }

    /**
     * 频道修改
     *
     * @param adChannel
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public ResponseResult updateChannel(AdChannel adChannel) {

        //缺少参数
        if (adChannel == null || adChannel.getId() == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        //无效参数
        if (adChannel.getName().length() > 10)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);


        //查询数据库中该频道数据
        AdChannel channel = super.getById(adChannel.getId());

        //数据库不存在该频道,无法修改返回'无效参数错误'
        if (channel == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);


        //假如用户传入了名字
        if (StringUtils.isNotEmpty(adChannel.getName())) {

            //查询数据库中该频道是否已经存在该名字
            LambdaQueryWrapper<AdChannel> lambdaQuery = Wrappers.lambdaQuery();
            lambdaQuery.eq(AdChannel::getName, adChannel.getName());
            AdChannel one = super.getOne(lambdaQuery);

            //已经存在,返回'数据已经存在'错误
            if (one != null && !one.getName().equals(adChannel.getName()))
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);

        }

        boolean updateResult = super.updateById(adChannel);

        //修改失败
        if (!updateResult)
            throw new CustomException(AppHttpCodeEnum.REMOTE_SERVER_ERROR);


        return ResponseResult.okResult();
    }

    /**
     * 根据Id删除频道
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public ResponseResult deleteChannel(Integer id) {
        if (id == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        AdChannel channel = super.getById(id);
        //数据库不存在该数据
        if (channel == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);

        return ResponseResult.okResult();
    }
}
